/**
 * @program: LeetCode
 * @description: LeetCode : 剑指 Offer 05. 替换空格
 * @author: WXY
 * @create: 2023-01-15 16:32
 * @Version 1.0
 **/
public class Num005_replaceSpace {
    public static String replaceSpace(String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' ') {
                sb.append("  ");
            }
        }
        if (sb.length() == 0) {
            return s;
        }
        int left = s.length() - 1;  //原始字符串的最后一个位置
        s += sb.toString(); //扩充空间
        int right = s.length() - 1; //扩充后字符串最后一个位置
        char[] str = s.toCharArray();
        while (left >= 0) {
            if (str[left] == ' ') {
                str[right--] = '0';
                str[right--] = '2';
                str[right] = '%';
            } else {
                str[right] = str[left];
            }
            left--;
            right--;
        }
        return new String(str);
    }

    public static void main(String[] args) {
        String s = "we is happy";
        String s1 = replaceSpace(s);
        System.out.println(s1);

    }
}
